

public class Grid {

	public int[][] grid;
	private Shape activeShape;
	private Shape pendingShape;
	private boolean checkLegal()
	{
		int x,y;
		for (int i = 0; i < activeShape.current.length; i++)
		{
			for (int j = 0; j < activeShape.current.length; j++)
			{
				if( activeShape.current[i][j] != 0)
				{
					x = j+activeShape.x_coord;
					y = i+activeShape.y_coord;
					if (x < 0)
						return false;
					if (y < 0)
						return false;
					if (x >= grid[0].length)
						return false;
					if (y >= grid.length)
						return false;
					if ( grid[y][x] != 0)
						return false;
				}
			}
		}
		return true;
	}
	public void moveLeft()
	{
		activeShape.moveLeft();
		if (!checkLegal())
			activeShape.moveRight();
	}
	public void moveRight()
	{
		activeShape.moveRight();
		if (!checkLegal())
			activeShape.moveLeft();
	}
	public synchronized int moveDown()
	{
		activeShape.moveDown();
		if (!checkLegal())
		{
			activeShape.moveUp();
			return addShape();
		}
		return 0;
	}
	public void rotate()
	{
		activeShape.rotate();
		if (!checkLegal())
		{
			activeShape.rotate();
			activeShape.rotate();
			activeShape.rotate();
		}
	}

	public Grid(int sizeX,int sizeY)
	{
		grid = new int[sizeY][sizeX];
		activeShape = new Shape((grid[0].length+1)/2-2,1);
		pendingShape = new Shape((grid[0].length+1)/2-2,1);
	}

	public int sizeActiveShape()
	{
		return activeShape.shapeSize();
	}

	public int sizePendingShape()
	{
		return pendingShape.shapeSize();
	}

	public int[][] getActiveShape()
	{
		return activeShape.current;
	}

	public int[][] getPendingShape()
	{
		return pendingShape.current;
	}

	public int getActiveShapeX()
	{
		return activeShape.x_coord;
	}

	public int getActiveShapeY()
	{
		return activeShape.y_coord;
	}

	public int addShape()
	{
		int x,y;
		int lines = 0;
		for (int i = 0; i < activeShape.current.length; i++)
		{
			for (int j = 0; j < activeShape.current.length; j++)
			{
				if( activeShape.current[i][j] != 0)
				{
					x = j+activeShape.x_coord;
					y = i+activeShape.y_coord;
					grid[y][x] = activeShape.current[i][j];
				}
			}
		}
		//shrink full lines
		for (int i = 0; i < grid.length; i++)
		{
			int j;
			for (j = 0; j < grid[0].length; j++)
			{
				if ( grid[i][j] == 0)
					break;	
			}
			if ( j >= grid[0].length )
			{
				java.util.Arrays.fill(grid[0],0);
				for (int k = i; k > 0; k--)
					System.arraycopy(grid[k-1],0,grid[k],0,grid[0].length);
				lines ++;
			}
		}
		activeShape = pendingShape;
		pendingShape = new Shape((grid[0].length+1)/2-2,0);
		return lines;
	}

	public String toString()
	{
		String str = "";
		for (int lines[]: this.grid)
		{
			for (int content : lines)
			{
				str += content + " ";
			}
			str += "\n";
		}
		return str;
	}

	// =======================================//
	// ============ Méthode de tests =========//
	// =======================================//
	public static void main(String[] args)
	{

		Grid g = new Grid(10,10);
		g.addShape();
		System.out.println(g.toString());
	}
}
